package com.antdesignbackend.backend.district.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.district.entity.District;
import com.antdesignbackend.backend.district.entity.entityVo.DistrictVo;
import com.antdesignbackend.backend.district.service.DistrictService;

/**
 * 区县管理
 *
 * @author 不二
 * @email jack.com
 * @date 2020-07-21 17:56:51
 */

@Controller
@RequestMapping("/district")
@Slf4j
public class DistrictController extends BaseController{

	private static final String FUN_NAME = "区县管理";//功能名称

	@Autowired
	private DistrictService districtService;

	@GetMapping("/index")
	@AuthToken
	public String index() {
		return "district/index";
	}


	@PostMapping("/tableSearch")
	@ResponseBody
	ReturnVO tableSearch(@RequestBody DistrictVo districtVo) throws SecurityException, IllegalArgumentException{
		ReturnVO result = new ReturnVO();
		QueryWrapper<DistrictVo> wrapper = new QueryWrapper<>();
		if(districtVo.getDistrict_name() != null && !districtVo.getDistrict_name().equals("")){
			wrapper.like("district_name",districtVo.getDistrict_name());
		}
		if (districtVo.getCity_id() != null && !districtVo.getCity_id().equals("") && !districtVo.getCity_id().equals("-1")){
			wrapper.eq("d.city_id",districtVo.getCity_id());
		}
		List<DistrictVo> list = new ArrayList<>();
		int total = districtService.count(wrapper);
		if (total > 0){
			list = districtService.search(wrapper,new Page<DistrictVo>(districtVo.getPageIndex(),districtVo.getPageSize()));
		}
		result.setTotal(total);
		result.setData(list);
		return result;

	}

	@GetMapping("/getById/{district_id}")
	@ResponseBody
	public	District getById(@PathVariable(name = "district_id") java.lang.String district_id) {
		District obj = districtService.getById(district_id);
		return obj;
	}

	@PostMapping("/save")
	@ResponseBody
	public ReturnVO save(@RequestBody District district){
		ReturnVO result = new ReturnVO();
		try {
			if(districtService.save(district)){
				result.setMessage("保存成功！");
				result.setCode("200");
			}else{
				result.setMessage("保存失败！");
				result.setCode("500");
			}
		}catch (Exception e){
			log.error(e.getMessage());
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	@PostMapping("update")
	@ResponseBody
	public ReturnVO updateById(@RequestBody District district){
		ReturnVO result = new ReturnVO();
		if(districtService.updateById(district)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	/**
     * 根据id删除
     */
	@DeleteMapping("/deleteById/{id}")
	@ResponseBody
	public ReturnVO deleteById(@PathVariable Serializable id) {
		ReturnVO result = new ReturnVO();
		if (districtService.removeById(Integer.parseInt(id.toString())) == true) {
			result.setMessage("删除成功");
		} else {
			result.setMessage("删除失败");
			result.setCode("0");
		}
		return result;
	}

	/**
	 * 根据城市编号查询所有区县
	 */
	@GetMapping("/getDistrictByCity")
	@ResponseBody
	public List<District> getDistrictByCity(@RequestParam(required = false,value = "city_id") String city_id) {
		QueryWrapper<District> wrapper = new QueryWrapper<>();
		wrapper.eq("city_id",city_id);
		return districtService.list(wrapper);
	}
}
